package by.pavlyukevich.paymentsystem.page;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;

import by.pavlyukevich.paymentsystem.lang.LanguageSelectionPanel;
import by.pavlyukevich.paymentsystem.login.LoginLogoutPanel;
import by.pavlyukevich.paymentsystem.panel.NavigationPanel;
import by.pavlyukevich.paymentsystem.webapp.BasicAuthenticationSession;

public abstract class BaseLayout extends WebPage {

	@Override
	protected void onInitialize() {
		super.onInitialize();

		add(new Label("headerTitle", getPageTitle()));
		add(new LoginLogoutPanel("loginLogout"));
		add(new LanguageSelectionPanel("langSelect"));
		add(new ListView<Pair<ResourceModel, WebPage>>("menu-nav", menuItems()) {

			@Override
			protected void populateItem(ListItem<Pair<ResourceModel, WebPage>> item) {
				item.add(new NavigationPanel("menu-nav-item", item.getModelObject()));
			}
		});
	}

	protected IModel<String> getPageTitle() {
		return new Model<String>(getClass().getSimpleName());
	}

	static List<Pair<ResourceModel, WebPage>> menuItems() {
		List<Pair<ResourceModel, WebPage>> itemList = new ArrayList<Pair<ResourceModel, WebPage>>();
		itemList.add(new MutablePair<ResourceModel, WebPage>(new ResourceModel("p.home.title"),
				new HomePage()));
		itemList.add(new MutablePair<ResourceModel, WebPage>(
				new ResourceModel("p.individual.title"), new IndividualPage()));
		if (BasicAuthenticationSession.get().isSignedIn()
				&& BasicAuthenticationSession.get().getRoles().contains("ADMIN")) {
			itemList.add(new MutablePair<ResourceModel, WebPage>(
					new ResourceModel("p.admin.title"), new AdminPage()));
		}

		return itemList;
	}

}